<?php

/**
 * forum actions.
 *
 * @package    bde
 * @subpackage forum
 * @author     Benjamin Herlin
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class forumActions extends sfActions
{
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeIndex(sfWebRequest $request)
	{
		$this->forward('default', 'module');
	}

	public function executeCategories(sfWebRequest $request)
	{
		$this->categories = CategoryTable::getAll();
	}

	public function executeCategory(sfWebRequest $request)
	{
		$category_id = $request->getParameter('category', null);

		$this->forward404Unless($category_id != null);

		$this->category = CategoryTable::getById($category_id);
		$this->subcategories = SubcategoryTable::getByCategory($category_id);
	}

	public function executeSubcategory(sfWebRequest $request)
	{
		$subcategory_id = $request->getParameter('subcategory');
		$page = $request->getParameter('page');
		
		$this->topics_by_page = sfConfig::get('app_topics_by_page');
		$this->total_topics = TopicTable::countInSubcategory($subcategory_id);
		$this->page = $page;

		$this->subcategory = SubcategoryTable::getById($subcategory_id);
		$this->topics = TopicTable::getBySubcategory($subcategory_id, $this->getUser()->getAttribute('id'), $page);
		$this->read = ReadTable::getBySubcategory($subcategory_id, $this->getUser()->getAttribute('id'));
	}

	public function executeTopic(sfWebRequest $request)
	{
		$topic_id = $request->getParameter('topic');
		$page = $request->getParameter('page');

		$this->posts_by_page = sfConfig::get('app_posts_by_page');
		$this->total_posts = PostTable::countInTopic($topic_id);
    	$this->page = $page;

		$this->getUser()->offsetSet('topic_id', $topic_id);
		
		$this->topic = TopicTable::getById($topic_id);
		$this->posts = PostTable::getById($topic_id, $page);
		$this->postform = new PostForm();
		
		// On indique que l'utilisateur a visité le topic
		// Pour gérer les messages lus/non lus
		ReadTable::setLastReadAtNow($topic_id, $this->getUser()->getAttribute('id'));
	}

	public function executeNewpost(sfWebRequest $request)
	{
		if ($request->isMethod(sfRequest::POST))
		{
			// Un formulaire a été envoyé
			$postform = new PostForm();

			$postform->bind($request->getParameter('post'));

			if ($postform->isValid())
			{
				$topic_id = $this->getUser()->offsetGet('topic_id');
				$this->getUser()->offsetUnset('topic_id');
				$this->forward404Unless(is_numeric($topic_id), "L'id du topic n'est pas valide.");

				// Création du post
				$newpost = new Post();
				$newpost->setContent($postform->getValue('content'));
				$newpost->setTopic($topic_id);
				$newpost->setOwner($this->getUser()->getAttribute('id'));

				// Sauvegarde du post
				$newpost->save();

				// Mise à jour du last_post du topic
				TopicTable::setLastPost($topic_id, $newpost->getDateTimeObject('updated_at'), $this->getUser()->getAttribute('nickname'));

				// Message d'information pour l'utilisateur
				$this->getUser()->setFlash('info', "Votre message a correctement été posté.");

				// Redirection vers le message fraîchement posté
				// TODO rediriger vers last page au lieu de 1ere
				// TODO ancre vers $newpost->getPrimaryKey()
				$this->redirect('topic', array('topic' => $topic_id, 'page' => 1));
			}
			else
			{
				$topic_id = $this->getUser()->offsetGet('topic_id');
				$this->forward404Unless(is_numeric($topic_id), "L'id du topic n'est pas valide.");

				$this->getUser()->setFlash('error', "Le formulaire n'est pas valide.");
				$this->redirect('topic', array('topic' => $topic_id, 'page' => 1));
			}
		}
		else
		{
			// Aucun formulaire n'a été posté
			$this->forward404("Impossible de poster un message via cette page");
		}
	}

	public function executeEditpost(sfWebRequest $request)
	{
		if ($request->isMethod(sfRequest::POST))
		{
			// Un formulaire a été envoyé
			$postform = new PostForm();

			$postform->bind($request->getParameter('post'));

			if ($postform->isValid())
			{
				$post_id = $this->getUser()->offsetGet('post_id');
				$this->getUser()->offsetUnset('post_id');
				$this->forward404Unless(is_numeric($post_id), "L'id du topic n'est pas valide.");

				$newpost = Doctrine_Core::getTable('Post')
				->findOneBy('id', $post_id);

				$newpost->setContent($postform->getValue('content'));
				$newpost->save();

				$this->getUser()->setFlash('info', "Votre message a correctement été édité.");

				$topic_id = $this->getUser()->offsetGet('topic_id');
				$this->forward404Unless(is_numeric($topic_id), "Impossible de rediriger vers le topic correct.");
				// TODO ancre vers $post_id
				// TODO rediriger vers la bonne page
				$this->redirect('topic', array('topic' => $topic_id, 'page' => 1));
			}
			else
			{
				$this->getUser()->setFlash('error', "Le formulaire n'est pas valide.");
				$this->post_id = $this->getUser()->offsetGet('post_id');
				$this->postform = $postform;
			}
		}
		else
		{
			// Aucun formulaire n'a été posté
			$id = $request->getParameter('id', null);
			$this->forward404Unless(is_numeric($id), "Id incorrect pour l'édition d'un post.");

			// On stocke l'id d'édition en session
			$this->getUser()->offsetSet('post_id', $id);

			$post = Doctrine_Core::getTable('Post')
			->findOneBy('id', $id);
			$this->post_id = $post->getId();

			$postform = new PostForm($post);
			$this->postform = $postform;
		}
	}

	public function executeNewtopic(sfWebRequest $request)
	{
		if ($request->isMethod(sfRequest::POST))
		{
			// Un formulaire a été envoyé
			$topic = new TopicForm();
			$topic->embedPostForm();

			$topic->bind($request->getParameter('topic'));

			if ($topic->isValid())
			{
				$subcategory_id = $this->getUser()->offsetGet('subcategory_id');
				$this->getUser()->offsetUnset('subcategory_id');
				$this->forward404Unless(is_numeric($subcategory_id), "L'id de la sous-catégorie n'est pas valide.");

				$post = $topic->getValue('post');

				// Création du topic
				$newtopic = new Topic();
				$newtopic->setName($topic->getValue('name'));
				$newtopic->setSynthesis($topic->getValue('synthesis'));
				$newtopic->setSubcategory($subcategory_id);
				$newtopic->setOwner($this->getUser()->getAttribute('id'));
				//$newtopic->setLastPostAt(date('Y-m-d H:i:s'));
				//$newtopic->setLastPostBy($this->getUser()->getAttribute('nickname'));
				$newtopic->save();

				// Création du post
				$newpost = new Post();
				$newpost->setContent($post['content']);
				$newpost->setTopic($newtopic->getPrimaryKey());
				$newpost->setOwner($this->getUser()->getAttribute('id'));
				$newpost->save();
				
				// Mise à jour du last_post du topic
				//TopicTable::setLastPost($newtopic->getPrimaryKey(), $newpost->getDateTimeObject('created_at'), $this->getUser()->getAttribute('nickname'));
				TopicTable::setLastPost($newtopic->getPrimaryKey(), new DateTime(date('Y-m-d H:i:s')), $this->getUser()->getAttribute('nickname'));

				$this->getUser()->setFlash('info', "Votre topic a correctement été posté.");

				
				// TODO ancre vers $newpost->getPrimaryKey()
				$this->redirect('topic', array('topic' => $newtopic->getPrimaryKey(), 'page' => 1));
			}
			else
			{
				$subcategory_id = $this->getUser()->offsetGet('subcategory_id');
				$this->forward404Unless(is_numeric($subcategory_id), "L'id de la sous-catégorie n'est pas valide.");

				$this->getUser()->setFlash('error', "Le formulaire n'est pas valide.");
				$this->topicform = $topic;
				$this->subcategory = $subcategory_id;
			}
		}
		else
		{
			// Aucun formulaire n'a été posté
			$subcategory = $request->getParameter('subcategory');
				
			$this->getUser()->offsetSet('subcategory_id', $subcategory);

			$topicform = new TopicForm();
			$topicform->embedPostForm();
			$this->topicform = $topicform;
			$this->subcategory = $subcategory;
		}
	}
	
	public function executeEdittopic(sfWebRequest $request)
	{
		if ($request->isMethod(sfRequest::POST))
		{
			// Un formulaire a été envoyé
			$topicform = new TopicForm();

			$topicform->bind($request->getParameter('topic'));

			if ($topicform->isValid())
			{
				$topic_id = $this->getUser()->offsetGet('topic_id');
				$this->getUser()->offsetUnset('topic_id');
				$this->forward404Unless(is_numeric($topic_id), "L'id du topic n'est pas valide.");

				$newtopic = Doctrine_Core::getTable('Topic')
				->findOneBy('id', $topic_id);

				$newtopic->setName($topicform->getValue('name'));
				$newtopic->setSynthesis($topicform->getValue('synthesis'));
				$newtopic->save();

				$this->getUser()->setFlash('info', "Votre topic a correctement été édité.");

				$this->redirect('topic', array('topic' => $topic_id, 'page' => 1));
			}
			else
			{
				$this->getUser()->setFlash('error', "Le formulaire n'est pas valide.");
				$topic_id = $this->getUser()->offsetGet('topic_id');
				$this->forward404Unless(is_numeric($topic_id), "Impossible de rediriger vers le topic correct.");
				//$this->redirect('topic', array('topic' => $topic_id, 'page' => 1));
				$this->topicform = $topicform;
				$this->topic = $topic_id;
			}
		}
		else
		{
			// Aucun formulaire n'a été posté
			$id = $request->getParameter('id', null);
			$this->forward404Unless(is_numeric($id), "Id incorrect pour l'édition d'un topic.");

			// On stocke l'id d'édition en session
			$this->getUser()->offsetSet('topic_id', $id);

			$topic = Doctrine_Core::getTable('Topic')
			->findOneBy('id', $id);

			$topicform = new TopicForm($topic);
			$this->topicform = $topicform;
			$this->topic = $id;
		}
	}
}
